package com.lottery.ruok.lottrey.chat.base;

import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.lottery.ruok.lottrey.chat.event.handler.OnConversationClickListener;
import com.lottery.ruok.lottrey.chat.event.handler.OnConversationClickListenerImpl;
import com.lottery.ruok.lottrey.chat.event.listener.ChatEventConversationClickListener;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;

public abstract class BaseChatActivity extends AppCompatActivity {

    private OnConversationClickListener onConversationClickListener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
        super.onCreate(savedInstanceState);
        init();
        initView();
        load();
    }

    protected abstract void init();

    protected abstract void initView();

    protected abstract void load();

    @Override
    public void setContentView(int layoutResID) {
        super.setContentView(layoutResID);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        /**
         * 注册聊天相关点击事件监听
         */
        onConversationClickListener = getOnConversationClickListener();
        ChatEventConversationClickListener.getIntance().registerOnConversationClickListener(onConversationClickListener);
    }

    /**
     * 可以通过复写该方法自己实现
     *
     * @return
     */
    protected OnConversationClickListener getOnConversationClickListener() {
        return new OnConversationClickListenerImpl();
    }

    @Override
    protected void onPause() {
        super.onPause();
        /**
         * 不再此页面释放监听事件
         */
        ChatEventConversationClickListener.getIntance().unRegisterOnConversationClickListener(onConversationClickListener);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private void initStatusBar() {
        Window win = getWindow();
        win.requestFeature(Window.FEATURE_NO_TITLE);
        win.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//去掉信息栏
        win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);// 保持屏幕高亮
        //KITKAT也能满足，只是SYSTEM_UI_FLAG_LIGHT_STATUS_BAR（状态栏字体颜色反转）只有在6.0才有效
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            win.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//透明状态栏
            // 状态栏字体设置为深色，SYSTEM_UI_FLAG_LIGHT_STATUS_BAR 为SDK23增加
            win.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

            // 部分机型的statusbar会有半透明的黑色背景
            win.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            win.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            win.setStatusBarColor(Color.TRANSPARENT);// SDK21
        }
    }
}
